<?php
class WebClient {
	
	public $timeOut = 10;
	
	/**
	 * Send a POST requst using cURL
	 * @param string $url to request
	 * @param array $post values to send
	 * @param array $options for cURL
	 * @return string
	 */
	public function post($url, array $post = array(), array $options = array()) {
		$defaults = array(
			CURLOPT_URL => $url,
			CURLOPT_HEADER => TRUE,
			CURLOPT_RETURNTRANSFER => TRUE,
			CURLOPT_TIMEOUT => $this->timeOut,

			CURLOPT_FRESH_CONNECT => 1,
			CURLOPT_POST => 1,
			CURLOPT_FORBID_REUSE => 1,
			CURLOPT_POSTFIELDS => http_build_query($post),
			// CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'),

			// CURLOPT_CAINFO => Yii::app()->basePath . DIRECTORY_SEPARATOR . 'cacert.pem',
			// CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0',
			// CURLOPT_ENCODING => 'gzip',
		);
		$ch = curl_init();
		curl_setopt_array($ch, ($defaults + $options)); 
		if( !$result = curl_exec($ch)) {
			trigger_error(curl_error($ch));
		}
		curl_close($ch);
		return $result;
	}

	/**
	 * Send a GET requst using cURL
	 * @param string $url to request
	 * @param array $get values to send
	 * @param array $options for cURL
	 * @return string
	 */
	public function get($url, array $get = array(), array $options = array()) {   
		$defaults = array(
			CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''). http_build_query($get),
			CURLOPT_HEADER => TRUE,
			CURLOPT_RETURNTRANSFER => TRUE,
			CURLOPT_TIMEOUT => $this->timeOut,
			
			// CURLOPT_CAINFO => Yii::app()->basePath . DIRECTORY_SEPARATOR . 'cacert.pem',
			// CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0',
			// CURLOPT_ENCODING => 'gzip',
		);
	   
		$ch = curl_init();
		curl_setopt_array($ch, ($defaults + $options)); 
		if( !$result = curl_exec($ch)) {
			trigger_error(curl_error($ch));
		}
		curl_close($ch);
		return $result;
	}
}